package com.example.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.education.Bean.RecommendInfo;
import com.example.education.Bean.StudentInfo;
import com.example.education.Mapper.StudentInfoMapper;
import com.example.education.service.StudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 姚
 * @since 2023-03-27
 */
@Service
@Transactional
public class StudentInfoServiceImpl extends ServiceImpl<StudentInfoMapper, StudentInfo> implements StudentInfoService {

    @Autowired
    private StudentInfoMapper studentInfoMapper;

    public int insert(StudentInfo studentInfo) {

        if (studentInfo.getUid() == null) {
            return studentInfoMapper.insert(studentInfo);
        } else {
            UpdateWrapper<StudentInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("uid", studentInfo.getUid());
            updateWrapper.set("grade", studentInfo.getGrade());
            return studentInfoMapper.update(studentInfo, updateWrapper);
        }

    }

    @Override
    public int delete(Integer uid) {
        return 0;
    }

    @Override
    public int update(StudentInfo studentInfo) {
        if (studentInfo.getUid() == null) {
            return studentInfoMapper.insert(studentInfo);
        } else {
            UpdateWrapper<StudentInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("uid", studentInfo.getUid());
            updateWrapper.set("grade", studentInfo.getGrade());
            return studentInfoMapper.update(studentInfo, updateWrapper);
        }
    }

    //根据uid查询
    @Override
    public StudentInfo select(Integer uid) {
        return studentInfoMapper.selectById(uid);
    }

    @Override
    public StudentInfo select(String username) {
        return studentInfoMapper.select(username);

    }

    //注册
    @Override
    public int register(StudentInfo studentInfo) {
        //判断是否已经注册
        if (studentInfoMapper.select(studentInfo.getPhone()) == null) {
            return studentInfoMapper.insert(studentInfo);
        } else {
            return 0;
        }

    }

    //登录
    @Override
    public int login(StudentInfo studentInfo) {
        //判断是否已经注册
        if (studentInfoMapper.select(studentInfo.getPhone()) == null) {
            return 0;
        } else {
            if (studentInfoMapper.select(studentInfo.getPhone()).getPassWord().equals(studentInfo.getPassWord())) {
                return 1;
            } else {
                return 0;
            }
        }
    }


    @Override
    public Integer grade(StudentInfo studentInfo, RecommendInfo recommendInfo) {

        Recommend recommend = new Recommend();
        Integer grade = recommend.jisuan(recommendInfo, studentInfo);
        studentInfo.setGrade(grade);

        //更新数据库
        UpdateWrapper<StudentInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("uid", studentInfo.getUid());
        updateWrapper.set("grade", studentInfo.getGrade());
        return studentInfoMapper.update(studentInfo, updateWrapper);
    }

}


